package com.tange.ai.iot.client.gnssa.utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;

public class ThumbnailResponse {
    public int count;
    public List<ThumbnailInfo> thumbnails = new ArrayList<>();

    public static class ThumbnailInfo {
        public int urlLength;
        public int timestamp;
        public String url;
    }

    public static class ResponseParser {

        public static String parseToJson(byte[] data) {
            ThumbnailResponse response = parseByteData(data);
            return new Gson().toJson(response);
        }

        private static ThumbnailResponse parseByteData(byte[] data) {
            // 创建字节缓冲区并尝试两种字节顺序
            ByteBuffer buffer = ByteBuffer.wrap(data);

            // 先尝试小端序
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            ThumbnailResponse response = tryParse(buffer);

            // 如果解析结果不合理，尝试大端序
            if (response.count < 0 || response.count > 50) {
                buffer.rewind(); // 重置缓冲区位置
                buffer.order(ByteOrder.BIG_ENDIAN);
                response = tryParse(buffer);
            }

            // 如果仍然无效，使用安全解析
            if (response.count < 0 || response.count > 50) {
                buffer.rewind();
                response = safeParse(buffer);
            }

            return response;
        }

        private static ThumbnailResponse tryParse(ByteBuffer buffer) {
            ThumbnailResponse response = new ThumbnailResponse();
            response.count = buffer.getInt();

            // 限制实际处理数量 (0-50)
            int actualCount = Math.max(0, Math.min(response.count, 50));

            for (int i = 0; i < 50; i++) {
                ThumbnailInfo info = new ThumbnailInfo();
                info.urlLength = buffer.getInt();
                info.timestamp = buffer.getInt();

                // 安全处理URL长度
                int safeLength = Math.max(0, Math.min(info.urlLength, 256));

                byte[] urlBytes = new byte[256];
                buffer.get(urlBytes);

                // 使用安全的长度创建字符串
                info.url = new String(urlBytes, 0, safeLength, StandardCharsets.UTF_8);

                if (i < actualCount) {
                    response.thumbnails.add(info);
                }
            }
            return response;
        }

        private static ThumbnailResponse safeParse(ByteBuffer buffer) {
            ThumbnailResponse response = new ThumbnailResponse();

            try {
                // 尝试读取count值，如果异常则设为0
                response.count = Math.max(0, Math.min(buffer.getInt(), 50));
            } catch (Exception e) {
                response.count = 0;
            }

            int actualCount = response.count;

            for (int i = 0; i < 50; i++) {
                try {
                    ThumbnailInfo info = new ThumbnailInfo();

                    // 安全读取整数
                    info.urlLength = Math.max(0, Math.min(buffer.getInt(), 256));
                    info.timestamp = buffer.getInt();

                    byte[] urlBytes = new byte[256];
                    buffer.get(urlBytes);

                    // 创建安全字符串
                    info.url = new String(urlBytes, 0, info.urlLength, StandardCharsets.UTF_8);

                    if (i < actualCount) {
                        response.thumbnails.add(info);
                    }
                } catch (Exception e) {
                    // 跳过损坏的结构体
                    if (buffer.remaining() >= 256 + 8) {
                        buffer.position(buffer.position() + 256 + 8);
                    } else {
                        break; // 没有足够数据时退出
                    }
                }
            }
            return response;
        }
    }
}
